﻿@using BootstrapBlazor.Components
@using Microsoft.AspNetCore.SignalR.Client
@using TrackSystem.IO.Formula
@using TrackSystem.IO.Logics

<Card Color="Color.Primary">
    <HeaderTemplate>
        <div class="col-6 col-sm-6col-md-6 col-lg-6">
            逻辑表达式  @logic.Name
        </div>
        <div class="col-6 col-sm-6col-md-6 col-lg-6">
            <Switch OnColor="Color.Success" OnText="使能" OffText="关闭"
                    @bind-value="@logic.Enabled"
                    OnValueChanged="@((a) => SetValue(logic.Name??="", a))" />
        </div>
    </HeaderTemplate>

    <BodyTemplate>
        <Switch OnColor="Color.Success" OnText="程序运行" OffText="程序停止"
                        @bind-value="@logic.condValue.bVal"/>
        <p class="card-text">表达式 ：@logic.condFormula</p>

        @for (int i = 0; i < logic.lstFormual.Count; i++)
        {
            <Divider />
            <p class="card-text text-info">逻辑表达式 : @logic.lstStrFormual[i]</p>
            @if (i < logic.lstValue.Count)
            {
                <p class="card-text text-danger">计算结果 : @logic.lstFormual[i].OutVar = @logic.lstValue[i]</p>
            }
        }
    </BodyTemplate>

    <FooterTemplate>

    </FooterTemplate>
</Card>

@code {
    [Parameter]
    public LogicItem logic { get; set; }

    [Parameter]
    public HubConnection hub { get; set; }

    /// <summary>
    /// 修改值
    /// </summary>
    /// <param name="name"></param>
    /// <param name="flag"></param>
    /// <returns></returns>
    private async Task SetValue(string name, bool flag)
    {
        try
        {
            if (hub.State == HubConnectionState.Connected)
                await hub.SendAsync("Assign", $"{name}_Enable", new OperandDef(flag));
        }
        catch (Exception _)
        {
            // ignored
        }
    }
}
